/* SPDX-License-Identifier: GPL-2.0 */

/*
 * bpf lsm hooks function wrapper for check return value
 */

static inline int check_ret(int ret)
{
	if (ret && !IS_ERR(ERR_PTR(ret)))
		ret = -EFAULT;
	return ret;
}

noinline int bpf_lsm_capable_wrapper(const struct cred *cred, struct user_namespace *ns,
							int cap, unsigned int opts)
{
	return check_ret(bpf_lsm_capable(cred, ns, cap, opts));
}

noinline int bpf_lsm_bprm_check_security_wrapper(struct linux_binprm *bprm)
{
	return check_ret(bpf_lsm_bprm_check_security(bprm));
}

#define bpf_lsm_bprm_committed_creds_wrapper bpf_lsm_bprm_committed_creds

noinline int bpf_lsm_sb_mount_wrapper(const char *dev_name, const struct path *path,
						const char *type, unsigned long flags, void *data)
{
	return check_ret(bpf_lsm_sb_mount(dev_name, path, type, flags, data));
}

#ifdef CONFIG_SECURITY_PATH
noinline int bpf_lsm_path_unlink_wrapper(const struct path *dir, struct dentry *dentry)
{
	return check_ret(bpf_lsm_path_unlink(dir, dentry));
}

noinline int bpf_lsm_path_mkdir_wrapper(const struct path *dir, struct dentry *dentry, umode_t mode)
{
	return check_ret(bpf_lsm_path_mkdir(dir, dentry, mode));
}

noinline int bpf_lsm_path_rmdir_wrapper(const struct path *dir, struct dentry *dentry)
{
	return check_ret(bpf_lsm_path_rmdir(dir, dentry));
}

noinline int bpf_lsm_path_mknod_wrapper(const struct path *dir, struct dentry *dentry,
						umode_t mode, unsigned int dev)
{
	return check_ret(bpf_lsm_path_mknod(dir, dentry, mode, dev));
}

noinline int bpf_lsm_path_symlink_wrapper(const struct path *dir, struct dentry *dentry,
						const char *old_name)
{
	return check_ret(bpf_lsm_path_symlink(dir, dentry, old_name));
}

noinline int bpf_lsm_path_link_wrapper(struct dentry *old_dentry, const struct path *new_dir,
						struct dentry *new_dentry)
{
	return check_ret(bpf_lsm_path_link(old_dentry, new_dir, new_dentry));
}

noinline int bpf_lsm_path_rename_wrapper(const struct path *old_dir, struct dentry *old_dentry,
					const struct path *new_dir, struct dentry *new_dentry)
{
	return check_ret(bpf_lsm_path_rename(old_dir, old_dentry, new_dir, new_dentry));
}

noinline int bpf_lsm_path_chmod_wrapper(const struct path *path, umode_t mode)
{
	return check_ret(bpf_lsm_path_chmod(path, mode));
}

noinline int bpf_lsm_path_chown_wrapper(const struct path *path, kuid_t uid, kgid_t gid)
{
	return check_ret(bpf_lsm_path_chown(path, uid, gid));
}

noinline int bpf_lsm_path_chroot_wrapper(const struct path *path)
{
	return check_ret(bpf_lsm_path_chroot(path));
}
#endif /* CONFIG_SECURITY_PATH */

noinline int bpf_lsm_inode_create_wrapper(struct inode *dir, struct dentry *dentry, umode_t mode)
{
	return check_ret(bpf_lsm_inode_create(dir, dentry, mode));
}

noinline int bpf_lsm_inode_link_wrapper(struct dentry *old_dentry, struct inode *dir,
						struct dentry *new_dentry)
{
	return check_ret(bpf_lsm_inode_link(old_dentry, dir, new_dentry));
}

noinline int bpf_lsm_inode_unlink_wrapper(struct inode *dir, struct dentry *dentry)
{
	return check_ret(bpf_lsm_inode_unlink(dir, dentry));
}

noinline int bpf_lsm_inode_mkdir_wrapper(struct inode *dir, struct dentry *dentry, umode_t mode)
{
	return check_ret(bpf_lsm_inode_mkdir(dir, dentry, mode));
}

noinline int bpf_lsm_inode_rmdir_wrapper(struct inode *dir, struct dentry *dentry)
{
	return check_ret(bpf_lsm_inode_rmdir(dir, dentry));
}

noinline int bpf_lsm_inode_mknod_wrapper(struct inode *dir, struct dentry *dentry,
						umode_t mode, dev_t dev)
{
	return check_ret(bpf_lsm_inode_mknod(dir, dentry, mode, dev));
}

noinline int bpf_lsm_inode_rename_wrapper(struct inode *old_dir, struct dentry *old_dentry,
						struct inode *new_dir, struct dentry *new_dentry)
{
	return check_ret(bpf_lsm_inode_rename(old_dir, old_dentry, new_dir, new_dentry));
}

noinline int bpf_lsm_inode_setxattr_wrapper(struct dentry *dentry, const char *name,
						const void *value, size_t size, int flags)
{
	return check_ret(bpf_lsm_inode_setxattr(dentry, name, value, size, flags));
}

noinline int bpf_lsm_inode_getxattr_wrapper(struct dentry *dentry, const char *name)
{
	return check_ret(bpf_lsm_inode_getxattr(dentry, name));
}

noinline int bpf_lsm_file_permission_wrapper(struct file *file, int mask)
{
	return check_ret(bpf_lsm_file_permission(file, mask));
}

noinline int bpf_lsm_file_alloc_security_wrapper(struct file *file)
{
	return check_ret(bpf_lsm_file_alloc_security(file));
}

noinline int bpf_lsm_file_ioctl_wrapper(struct file *file, unsigned int cmd, unsigned long arg)
{
	return check_ret(bpf_lsm_file_ioctl(file, cmd, arg));
}

noinline int bpf_lsm_mmap_file_wrapper(struct file *file, unsigned long reqprot,
						unsigned long prot, unsigned long flags)
{
	return check_ret(bpf_lsm_mmap_file(file, reqprot, prot, flags));
}

noinline int bpf_lsm_file_mprotect_wrapper(struct vm_area_struct *vma, unsigned long reqprot,
						unsigned long prot)
{
	return check_ret(bpf_lsm_file_mprotect(vma, reqprot, prot));
}

noinline int bpf_lsm_file_lock_wrapper(struct file *file, unsigned int cmd)
{
	return check_ret(bpf_lsm_file_lock(file, cmd));
}

noinline int bpf_lsm_file_fcntl_wrapper(struct file *file, unsigned int cmd, unsigned long arg)
{
	return check_ret(bpf_lsm_file_fcntl(file, cmd, arg));
}

noinline int bpf_lsm_file_receive_wrapper(struct file *file)
{
	return check_ret(bpf_lsm_file_receive(file));
}

noinline int bpf_lsm_file_open_wrapper(struct file *file)
{
	return check_ret(bpf_lsm_file_open(file));
}

#ifdef CONFIG_SECURITY_NETWORK
noinline int bpf_lsm_socket_create_wrapper(int family, int type, int protocol, int kern)
{
	return check_ret(bpf_lsm_socket_create(family, type, protocol, kern));
}

noinline int bpf_lsm_socket_bind_wrapper(struct socket *sock, struct sockaddr *address, int addrlen)
{
	return check_ret(bpf_lsm_socket_bind(sock, address, addrlen));
}

noinline int bpf_lsm_socket_connect_wrapper(struct socket *sock, struct sockaddr *address,
						int addrlen)
{
	return check_ret(bpf_lsm_socket_connect(sock, address, addrlen));
}

noinline int bpf_lsm_socket_listen_wrapper(struct socket *sock, int backlog)
{
	return check_ret(bpf_lsm_socket_listen(sock, backlog));
}

noinline int bpf_lsm_socket_accept_wrapper(struct socket *sock, struct socket *newsock)
{
	return check_ret(bpf_lsm_socket_accept(sock, newsock));
}

noinline int bpf_lsm_socket_sendmsg_wrapper(struct socket *sock, struct msghdr *msg, int size)
{
	return check_ret(bpf_lsm_socket_sendmsg(sock, msg, size));
}

noinline int bpf_lsm_socket_recvmsg_wrapper(struct socket *sock, struct msghdr *msg,
						int size, int flags)
{
	return check_ret(bpf_lsm_socket_recvmsg(sock, msg, size, flags));
}

noinline int bpf_lsm_socket_getsockopt_wrapper(struct socket *sock, int level, int optname)
{
	return check_ret(bpf_lsm_socket_getsockopt(sock, level, optname));
}

noinline int bpf_lsm_socket_setsockopt_wrapper(struct socket *sock, int level, int optname)
{
	return check_ret(bpf_lsm_socket_setsockopt(sock, level, optname));
}
#endif /* CONFIG_SECURITY_NETWORK */
